package us.nchu.tomtiger.dao;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;
import us.nchu.tomtiger.model.Project;
import us.nchu.tomtiger.model.User;
import us.nchu.tomtiger.rowMapper.ProjectRowMapper;
import us.nchu.tomtiger.rowMapper.UserRowMapper;

import javax.annotation.Resource;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

/**
 * Created by XiongXuan on 2018/1/8.
 */
@Repository
public class ProjectDao {
    @Resource
    private JdbcTemplate jdbcTemplate;

    public List<Project> getListForSqlString(String innerSql, int status) {
        String sql = "select * from project where " + innerSql + " and status=" + status;

        return jdbcTemplate.query(sql, new ProjectRowMapper());
    }

    public List<Project> queryAll() {
        String sql = "select * from project";

        return jdbcTemplate.query(sql, new ProjectRowMapper());
    }


    public boolean insert(Project project) {
        String sql = "insert into project(owner, name, type, field, intro, contact_type, contact, status) values(?,?,?,?,?,?,?,?);";

        Object[] objects = new Object[] {
                project.getPhoneNumber(),
                project.getName(),
                project.getType(),
                project.getField(),
                project.getIntro(),
                project.getContactType(),
                project.getContact(),
                0
        };

        return jdbcTemplate.update(sql, objects) > 0;
    }

    public void checkProject(int projectId) {
        String sql = "update project set status=1 where id=" + projectId;
        jdbcTemplate.update(sql);
    }

    public Project queryByOwner(String phoneNumber) {
        Project project;

        String sql = "select * from project where owner=" + phoneNumber + " and status=1;";
        project = (Project) jdbcTemplate.queryForObject(sql, new ProjectRowMapper());

        return project;
    }

    public Project queryById(int id) {
        Project project;

        String sql = "select * from project where id=" + id + " and status=1;";
        project = (Project) jdbcTemplate.queryForObject(sql, new ProjectRowMapper());

        return project;
    }

    public Project queryById(String id) {
        Project project;

        String sql = "select * from project where id=" + id + " and status=1;";
        project = (Project) jdbcTemplate.queryForObject(sql, new ProjectRowMapper());

        return project;
    }

    public boolean update(Project project) {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("update project set");

        stringBuilder.append(" name='" + project.getName() + "'");
        stringBuilder.append(",owner='" + project.getPhoneNumber() + "'");
        stringBuilder.append(",type=" + project.getType());
        stringBuilder.append(",field='" + project.getField() + "'");
        stringBuilder.append(",contact_type=" + project.getContactType());
        stringBuilder.append(",contact='" + project.getContact() + "'");
        stringBuilder.append(",intro='" + project.getIntro() + "'");
        stringBuilder.append(" where id=" + project.getId() + " and status=1;");

        System.out.println(stringBuilder.toString());

        return jdbcTemplate.update(stringBuilder.toString()) > 0;
    }

    public boolean deleteById(String id) {
        String sql = "update project set status=0 where id=" + id + " and status=1";
        return jdbcTemplate.update(sql) > 0;
    }

    public boolean deleteById(int id) {
        String sql = "update project set status=0 where id=" + id + " and status=1";
        return jdbcTemplate.update(sql) > 0;
    }
}
